let achievements = [
  { name: "持之以恒", src: "../../assets/image/cj-1.png" },
  { name: "孜孜不倦", src: "../../assets/image/cj-2.png" },
  { name: "我是学霸", src: "../../assets/image/cj-3.png" },
  { name: "常胜将军", src: "../../assets/image/cj-4.png" },
  { name: "我是学神", src: "../../assets/image/cj-2.png" },
  { name: "耶耶耶耶", src: "../../assets/image/cj-4.png" },
  { name: "发愤忘食", src: "../../assets/image/cj-3.png" },
  { name: "悬梁刺股", src: "../../assets/image/cj-1.png" },
  { name: "好学不倦", src: "../../assets/image/cj-2.png" },
  { name: "专心致志", src: "../../assets/image/cj-3.png" },
];
class MockService {
  // 登录注册验证码
  count = null;
  getcode(obj) {
    if (!obj) {
      return Promise.resolve({
        status: 1017,
        msg: "参数错误",
      });
    } else if (typeof obj.phone !== "string") {
      return Promise.resolve({
        status: 1017,
        msg: "参数错误",
      });
    }
    this.count = "";
    for (let i = 0; i < 6; i++) {
      this.count += parseInt(Math.random() * 10);
    }
    return Promise.resolve({
      status: 0,
      code: this.count,
    });
  }
  // 登录注册，传入对象，对象属性为phone都为string类型
  Login(obj) {
    if (!obj) {
      return Promise.resolve({
        status: 1016,
        msg: "参数错误",
      });
    } else if (typeof obj.phone !== "string" && typeof obj.code !== "string") {
      return Promise.resolve({
        status: 1016,
        msg: "参数错误",
      });
    }
    let userList = JSON.parse(localStorage.getItem("userList"))
      ? JSON.parse(localStorage.getItem("userList"))
      : [];

    if (userList.length > 0) {
      for (let i = 0; i < userList.length; i++) {
        if (userList[i].phone == obj.phone && obj.code == this.count) {
          return Promise.resolve({
            status: 0,
            msg: "成功",
            token: userList[i].token,
          });
        }
      }
    }
    if (obj.code == this.count) {
      let num = parseInt(Math.random() * 10) + 2;
      // 初始化我的成就,随机成就
      let achievement = achievements
        .sort(function () {
          return 0.5 - Math.random();
        })
        .slice(0, num);
      userList.push({
        phone: obj.phone,
        cusername: obj.cusername ? obj.cusername : "",
        username: obj.username ? obj.username : "",
        birthday: obj.birthday ? obj.birthday : "",
        sex: obj.sex ? obj.sex : "",
        achievement,
        class: [],
        token: obj.phone + Date.now(),
        myMoney: parseInt(Math.random() * 1000),
        checkout: {
          checkout1: false,
          checkout2: false,
          checkout3: false,
          checkout4: false,
        },
      });

      localStorage.setItem("userList", JSON.stringify(userList));
      return Promise.resolve({
        status: 0,
        msg: "注册成功",
        token: userList.pop().token,
      });
    } else {
      return Promise.resolve({
        status: 1016,
        msg: "验证码错误",
      });
    }
  }
  // 注册完善信息以及初始化成就 传入对象，对象属性为token,sex,cusername,username,birthday都为string类型
  register(obj) {
    // 获取用户数据
    if (!obj) {
      return Promise.resolve({
        status: 1016,
        msg: "参数错误",
      });
    } else if (
      typeof obj.token !== "string" &&
      typeof obj.sex !== "string" &&
      typeof obj.birthday !== "string" &&
      typeof obj.username !== "string" &&
      typeof obj.cusername !== "string"
    ) {
      return Promise.resolve({
        status: 1016,
        msg: "参数错误",
      });
    }
    let userList = JSON.parse(localStorage.getItem("userList"));
    if (userList) {
      for (let i = 0; i < userList.length; i++) {
        if (userList[i].token == obj.token) {
          userList[i].sex = obj.sex;
          userList[i].cusername = obj.cusername;
          userList[i].username = obj.username;
          userList[i].birthday = obj.birthday;
        }
      }
    }
    localStorage.setItem("userList", JSON.stringify(userList));

    return Promise.resolve({
      status: 0,
      msg: "添加成功",
    });
  }
  // 添加课程 传入对象，对象属性为token(string),课程id(number)
  addClass(obj) {
    if (!obj) {
      return Promise.resolve({
        status: 1016,
        msg: "参数错误",
      });
    } else if (typeof obj.token !== "string" && typeof obj.id !== "string") {
      return Promise.resolve({
        status: 1016,
        msg: "参数错误",
      });
    }
    let userList = JSON.parse(localStorage.getItem("userList"));
    if (userList) {
      for (let i = 0; i < userList.length; i++) {
        if (userList[i].token == obj.token) {
          userList[i].class.push(obj.id);
          userList[i].class = [...new Set(userList[i].class)];
        }
      }
      localStorage.setItem("userList", JSON.stringify(userList));
      return Promise.resolve({
        status: 0,
        msg: "添加成功",
      });
    }
    return Promise.resolve({
      status: 1016,
      msg: "添加失败",
    });
  }
  // 取消课程
  delClass(obj) {
    if (!obj) {
      return Promise.resolve({
        status: 1016,
        msg: "参数错误",
      });
    } else if (typeof obj.token !== "string" && typeof obj.id !== "string") {
      return Promise.resolve({
        status: 1016,
        msg: "参数错误",
      });
    }
    let userList = JSON.parse(localStorage.getItem("userList"));

    for (let i = 0; i < userList.length; i++) {
      if (userList[i].token == obj.token) {
        userList[i].class = userList[i].class.filter(ele => ele !== obj.id);
      }
    }
    localStorage.setItem("userList", JSON.stringify(userList));
    return Promise.resolve({
      status: 0,
      msg: "删除成功",
    });
  }
  // 获取课程 传入对象，对象属性为token(string)
  getClass(obj) {
    if (!obj) {
      return Promise.resolve({
        status: 1016,
        msg: "参数错误",
      });
    } else if (typeof obj.token !== "string") {
      return Promise.resolve({
        status: 1016,
        msg: "参数错误",
      });
    }
    let userList = JSON.parse(localStorage.getItem("userList"));

    if (userList) {
      for (let i = 0; i < userList.length; i++) {
        if (userList[i].token == obj.token) {
          return Promise.resolve({
            status: 0,
            msg: "成功",
            results: userList[i].class,
          });
        }
      }
    }
    return Promise.resolve({
      status: 1016,
      msg: "失败",
    });
  }
  // 点击课程
  getClassId(obj) {
    if (!obj) {
      return Promise.resolve({
        status: 1016,
        msg: "参数错误",
      });
    } else if (typeof obj.token !== "string") {
      return Promise.resolve({
        status: 1016,
        msg: "参数错误",
      });
    }
    let userList = JSON.parse(localStorage.getItem("userList"));
    if (userList) {
      for (let i = 0; i < userList.length; i++) {
        if (userList[i].token == obj.token) {
          for (let j = 0; j < userList[i].class.length; j++) {
            if (obj.id == userList[i].class[j].id) {
              return Promise.resolve({
                status: 0,
                msg: "成功",
                results: userList[i].class[j],
              });
            }
          }
        }
      }
    }
    return Promise.resolve({
      status: 0,
      msg: "失败",
    });
  }
  // 获取我的信息
  getMimeInfo(obj) {
    if (!obj) {
      return Promise.resolve({
        status: 1016,
        msg: "参数错误",
      });
    } else if (typeof obj.token !== "string") {
      return Promise.resolve({
        status: 1016,
        msg: "参数错误",
      });
    }
    let userList = JSON.parse(localStorage.getItem("userList"));
    if (userList) {
      for (let i = 0; i < userList.length; i++) {
        if (userList[i].token == obj.token) {
          return Promise.resolve({
            status: 0,
            msg: "成功",
            results: userList[i],
          });
        }
      }
    }
    return Promise.resolve({
      status: 1016,
      msg: "失败",
    });
  }
  // 获取我的成就
  getAchievement(obj) {
    if (!obj) {
      return Promise.resolve({
        status: 1016,
        msg: "参数错误",
      });
    } else if (typeof obj.token !== "string") {
      return Promise.resolve({
        status: 1016,
        msg: "参数错误",
      });
    }
    let userList = JSON.parse(localStorage.getItem("userList"));
    if (userList) {
      for (let i = 0; i < userList.length; i++) {
        if (userList[i].token == obj.token) {
          return Promise.resolve({
            status: 0,
            msg: "成功",
            results: userList[i].achievement,
          });
        }
      }
    }
    return Promise.resolve({
      status: 1016,
      msg: "失败",
    });
  }
  //获取设置
  getSetInfo(obj) {
    if (!obj) {
      return Promise.resolve({
        status: 1016,
        msg: "参数错误",
      });
    } else if (typeof obj.token !== "string") {
      return Promise.resolve({
        status: 1016,
        msg: "参数错误",
      });
    }
    let userList = JSON.parse(localStorage.getItem("userList"));
    for (let i = 0; i < userList.length; i++) {
      if (userList[i].token == obj.token) {
        return Promise.resolve({
          status: 0,
          msg: "成功",
          results: userList[i].checkout,
        });
      }
    }
    return Promise.resolve({
      status: 1016,
      msg: "失败",
    });
  }
  // 修改设置
  updataSet(obj) {
    if (!obj) {
      return Promise.resolve({
        status: 1016,
        msg: "参数错误",
      });
    } else if (typeof obj.token !== "string" && typeof obj.checkout !== "string") {
      return Promise.resolve({
        status: 1016,
        msg: "参数错误",
      });
    }
    let userList = JSON.parse(localStorage.getItem("userList"));
    for (let i = 0; i < userList.length; i++) {
      if (userList[i].token == obj.token) {
        userList[i].checkout = JSON.parse(obj.checkout);
        localStorage.setItem("userList", JSON.stringify(userList));
        return Promise.resolve({
          status: 0,
          msg: "成功",
        });
      }
    }
    return Promise.resolve({
      status: 1016,
      msg: "失败",
    });
  }
}
export const mockService = new MockService();
